﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework.Input;
using DC2010.Objects;
using DC2010;
using DC2010.Objects.Base;
using DC2010.Effects;
using DC2010.Objects.Data;

namespace RolePlaying.Events
{
    public class EventsActionPanel
    {
        public EventsActionPanel()
        {
            RM.Get("Action_0").OnMouseLeft += new ActiveRectangle.mouseStateDelegate(EventsActionPanel_OnMouseLeft);
            RM.Get("Action_1").OnMouseLeft += new ActiveRectangle.mouseStateDelegate(EventsActionPanel_OnMouseLeft);
            RM.Get("Action_2").OnMouseLeft += new ActiveRectangle.mouseStateDelegate(EventsActionPanel_OnMouseLeft);
            RM.Get("Action_3").OnMouseLeft += new ActiveRectangle.mouseStateDelegate(EventsActionPanel_OnMouseLeft);

            //1 - Right
            //2 - Left
            RM.Get("ActionPanel_11").OnMouseLeft += new ActiveRectangle.mouseStateDelegate(RM_OnActionPanelMouseLeft);
            RM.Get("ActionPanel_12").OnMouseLeft += new ActiveRectangle.mouseStateDelegate(RM_OnActionPanelMouseLeft);
            RM.Get("ActionPanel_21").OnMouseLeft += new ActiveRectangle.mouseStateDelegate(RM_OnActionPanelMouseLeft);
            RM.Get("ActionPanel_22").OnMouseLeft += new ActiveRectangle.mouseStateDelegate(RM_OnActionPanelMouseLeft);
            RM.Get("ActionPanel_31").OnMouseLeft += new ActiveRectangle.mouseStateDelegate(RM_OnActionPanelMouseLeft);
            RM.Get("ActionPanel_32").OnMouseLeft += new ActiveRectangle.mouseStateDelegate(RM_OnActionPanelMouseLeft);
            RM.Get("ActionPanel_41").OnMouseLeft += new ActiveRectangle.mouseStateDelegate(RM_OnActionPanelMouseLeft);
            RM.Get("ActionPanel_42").OnMouseLeft += new ActiveRectangle.mouseStateDelegate(RM_OnActionPanelMouseLeft);


        }

        void EventsActionPanel_OnMouseLeft(MouseState state, ActiveRectangle ar)
        {
            CreatureData ccc = null;
            ItemData iii = null;

            if (bCurrentState)
            {
                Root.I.GameSystem.IsActionsActivated(ccc, iii);
                string sNumber = ar.Name.Replace("Action_","");
                int iNumber = Int32.Parse(sNumber);

                Root.I.GameSystem.LogDebug(ar.Name.ToUpper());

                //BaseObject bo = Root.I.GameSystem.DetermineTarget();

                //SimpleEffect sef = iii.Info.UsageEffects[iNumber);
                //Effect ef = Root.I.IS.InstantiatedEffect(sef);
                //ef.Instant(ccc, bo);

                Root.I.GameSystem.DeactivateAllActions();
            }
        }

        bool bCurrentState = false;
        bool bLastState = false;

        void RM_OnActionPanelMouseLeft(MouseState state, ActiveRectangle ar)
        {
            CreatureData ccc = null;
            ItemData iii = null;

            bLastState = bCurrentState;
            bCurrentState = Root.I.GameSystem.IsActionsActivated(ccc, iii);

            if (!bCurrentState)
            {
                //get party member
                string temp = ar.Name.Replace("ActionPanel_", "");
                string sMemberNumber = temp[0].ToString();
                int iMemberNumber = Int32.Parse(sMemberNumber);

                //get hand
                string sHand = temp[1].ToString();

                if (Root.I.GameSystem.Party.MembersData.Count >= iMemberNumber)
                {
                    CreatureData c = Root.I.GameSystem.Party.GetCreatureByIndex(iMemberNumber);

                    if (c != null)
                    {
                        ItemData iRH = c.Inventory.GetItemAtPosition(CarryLocations.RightHand).GetItemDataFromCode();
                        ItemData iLH = c.Inventory.GetItemAtPosition(CarryLocations.LeftHand).GetItemDataFromCode();

                        if ((sHand == "1")&&(iRH !=null)) //Right
                        {
                            if (iRH.Combo.Count > 0)
                                c.ActionOptionsAreDisplayed_Right = !c.ActionOptionsAreDisplayed_Right;
                        }

                        if ((sHand == "2") && (iLH != null)) //Left
                        {
                            if (iLH.Combo.Count > 0)
                                c.ActionOptionsAreDisplayed_Left = !c.ActionOptionsAreDisplayed_Left;
                        }
                    }
                }
            }
        }
    }
}
